/*
 * ExternalInputRelay.cpp
 *
 *  Created on: 2012. 10. 16.
 *      Author: dalxx
 */
#include <Arduino.h>
#include "RelayDI.h"
#undef SERIAL_TRACE_ON
namespace PLCLib {


RelayDI::RelayDI() {
	this->numRelay=0;
	this->relayList=NULL;
	this->pinList=NULL;
}



RelayDI::~RelayDI() {
	this->removeAll();
}


void RelayDI::init(int num_relay, Relay* relay_list) {
	this->removeAll();
	this->numRelay=num_relay;
	this->relayList=relay_list;
	this->pinList=(uint8_t*)malloc(num_relay*sizeof(uint8_t));
}

void RelayDI::removeAll() {
	free(this->pinList);
	this->numRelay=0;
	this->relayList=NULL;
	this->pinList=NULL;
}

void RelayDI::calculateStatus() {
	int i;
	for(i=0;i<this->numRelay;i++)
	{
		this->relayList[i].status=digitalRead(this->pinList[i]);
//#ifdef  SERIAL_TRACE_ON
//	Serial.print(" index: ");
//	Serial.print(i);
//	Serial.print(" pin: ");
//	Serial.print(this->portList[i]);
//	Serial.print(" value: ");
//	Serial.print(this->relayList[i].status);
//	Serial.println();
//
//#endif
	}
}

void RelayDI::setRelay(int index, uint8_t pin) {
	this->pinList[index]=pin;
	pinMode(this->pinList[index], INPUT);
#ifdef  SERIAL_TRACE_ON
	Serial.print(" index: ");
	Serial.print(index);
	Serial.print(" pin: ");
	Serial.print(this->pinList[index]);
	Serial.print(" value: ");
	Serial.print(this->relayList[index].status);
	Serial.println();

#endif
}

} /* namespace PLCLib */


